﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace IT_Pro.Class
{
    class MT_MayViTinh
    {
        private string _maMayTinh;

        public string MaMayTinh
        {
            get { return _maMayTinh; }
            set { _maMayTinh = value; }
        }
        private DateTime _ngayMua;

        public DateTime NgayMua
        {
            get { return _ngayMua; }
            set { _ngayMua = value; }
        }
        private DateTime _ngayHetHan;

        public DateTime NgayHetHan
        {
            get { return _ngayHetHan; }
            set { _ngayHetHan = value; }
        }
        private string _maNhaCC;

        public string MaNhaCC
        {
            get { return _maNhaCC; }
            set { _maNhaCC = value; }
        }
        private string _tinhTrang;

        public string TinhTrang
        {
            get { return _tinhTrang; }
            set { _tinhTrang = value; }
        }

        public static bool ThemMayTinh(MT_MayViTinh _maytinh)
        {
            string sql = "select * from MT_MayViTinh where MaMay='" + _maytinh.MaMayTinh + "'";
            DataTable dt = DataServices.ExecQuery(sql);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            sql = "insert into MT_MayViTinh values ('" + _maytinh.MaMayTinh + "','" + _maytinh.NgayMua + "','" + _maytinh.NgayHetHan + "','" + _maytinh.MaNhaCC + "','" + _maytinh.TinhTrang + "')";
            DataServices.ExecNonQuery(sql);
            return true;
        }

        public static bool XoaMayTinh(string _maMay)
        {
            string sql = "select * from MT_ChiTietMayTinh where MaMay='" + _maMay + "'";
            DataTable dt = DataServices.ExecQuery(sql);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            sql = "delete MT_MayViTinh where MaMay='" + _maMay + "'";
            DataServices.ExecNonQuery(sql);
            return true;
        }

        public static DataTable HienthidsDL(string _filldb)
        {
            string sql = "select * from " + _filldb + "";
            return DataServices.ExecQuery(sql);
        }
        public static DataTable TimMayTinh(string mamay)
        {
            string strSQL = "select * from MT_MayViTinh where MaMay='"+mamay+"'";
            return DataServices.ExecQuery(strSQL);
        }

        public static DataTable HienthiAllMayTinh()
        {
            string sql = "select (mt.MaMay+'-'+ ncc.TenNhaCC +N'  |  Ngày mua: '+ convert(varchar,mt.NgayMua,101)+N'  |  Ngày hết hạn: '+ convert(varchar,mt.NgayHetHan,101) +N' | Người dùng: ' + (select nv.TenNV from NV_NhanVien nv inner join CV_BanGiaoThietBi bg on nv.MaNV=bg.NguoiNhan "
		                    +"where bg.Giu=1 and bg.MaMay=mt.MaMay)) as MayTinh,dmlk.TenLK, " 
                            +"ct.HanBaoHanh,ct.NhaCungCap as LKNhaCC, tt.TenTT,tt.IDTT "
                            +"from MT_MayViTinh mt,MT_ChiTietMayTinh ct,MT_NhaCungCap ncc,MT_TinhTrang tt,MT_DanhMucLinhKien dmlk " 
                            +"where mt.MaMay=ct.MaMay and mt.NhaCungCap=ncc.MaNhaCC and mt.TinhTrang=tt.IDTT and ct.LinhKien=dmlk.MaLK";
		                        
            return DataServices.ExecQuery(sql);
        }
    }


    class MT_CTMayViTinh
    {
        private string _maCTMayTinh;

        public string MaCTMayTinh
        {
            get { return _maCTMayTinh; }
            set { _maCTMayTinh = value; }
        }

        private string _maMay;

        public string MaMay
        {
            get { return _maMay; }
            set { _maMay = value; }
        }

        private string _maLinhKien;

        public string MaLinhKien
        {
            get { return _maLinhKien; }
            set { _maLinhKien = value; }
        }
        private DateTime _hanBaoHanh;

        public DateTime HanBaoHanh
        {
            get { return _hanBaoHanh; }
            set { _hanBaoHanh = value; }
        }
        private string _nhaCC;

        public string NhaCC
        {
            get { return _nhaCC; }
            set { _nhaCC = value; }
        }

        public static bool ThemCTMayTinh(MT_CTMayViTinh _ctMaytinh)
        {
            string sql = "insert into MT_ChiTietMayTinh (MaMay,LinhKien,HanBaoHanh,NhaCungCap) values ('" + _ctMaytinh.MaMay + "','" + _ctMaytinh.MaLinhKien + "','" + _ctMaytinh.HanBaoHanh + "','" + _ctMaytinh.NhaCC + "')";
            DataServices.ExecNonQuery(sql);
            return true;
        }

        public static void XoaCTMay(string _maCTMay)
        {
            string sql = "delete MT_ChiTietMayTinh where IDChiTiet='" + _maCTMay + "'";
            DataServices.ExecNonQuery(sql);
        }

        public static DataTable HienthidsCTMay(string _maMay)
        {
            string sql = "select ct.IDChiTiet,dmlk.TenLK,ct.HanBaoHanh from MT_MayViTinh mt,MT_ChiTietMayTinh ct,MT_DanhMucLinhKien dmlk where mt.MaMay=ct.MaMay and ct.LinhKien=dmlk.MaLK and mt.MaMay='" + _maMay + "'";
            return DataServices.ExecQuery(sql);
        }
        public static DataTable HienthidsCTMayInfo(string _maMay)
        {
            string sql = "select 0 as STT,* , ct.NhaCungCap as NhaCC from MT_MayViTinh mt,MT_ChiTietMayTinh ct,MT_DanhMucLinhKien dmlk where mt.MaMay=ct.MaMay and ct.LinhKien=dmlk.MaLK and mt.MaMay='" + _maMay + "'";
            return DataServices.ExecQuery(sql);
        }
    }
}
